<?php
/** 初始化项目 */
require '../../app/app.php';
/** 初始化数据库 */
use app\util\Mysql;
$database = Mysql::init();
if(isset($_SESSION['username']) && $_SESSION['username'] != null){
    $username = $_SESSION['username'];
    $newpassword = $_POST['newpassword'];
    $oldpassword = $_POST['oldpassword'];
    $id = $database->select("user_text", "id", ["username" => $username]);
    $id = $id[0];
    $salt=$database->select("pass_key", "salt", ["uid" => $id]);
    $salt=$salt[0];
    $pass1= getPassword($oldpassword,$salt);
    $pass2= getPassword($newpassword,$salt);
    $pass0= $database->select("user_text", "password", ["username" => $username]);
    $pass0= $pass0[0];
    if($pass1==$pass0){
        $database->update("user_text", ["password" => $pass2], ["username" => $username]);
        echo "修改成功!";
    }else{
        echo "原密码错误!";
    }
}
else{
    echo "<script>alert('请登录!');location.href='/?c=login';</script>";
}
